package auditd

import (
	"github.com/google/btree"
	"sync"
)

type AuditBtree struct {
	*btree.BTree
	*sync.Mutex
}

func (a *AuditBtree) Clear(addNodesToFreelist bool) {
	a.Mutex.Lock()
	defer a.Mutex.Unlock()
	a.BTree.Clear(addNodesToFreelist)
}

func (a *AuditBtree) ReplaceOrInsert(item btree.Item) btree.Item {
	a.Mutex.Lock()
	defer a.Mutex.Unlock()
	return a.BTree.ReplaceOrInsert(item)
}

func (a *AuditBtree) DeleteMin() btree.Item {
	a.Mutex.Lock()
	defer a.Mutex.Unlock()
	return a.BTree.DeleteMin()
}

func (a *AuditBtree) Len() int {
	a.Mutex.Lock()
	defer a.Mutex.Unlock()
	return a.BTree.Len()
}

func (a *AuditBtree) Delete(item btree.Item) btree.Item {
	a.Mutex.Lock()
	defer a.Mutex.Unlock()
	return a.BTree.Delete(item)
}

func (a *AuditBtree) Ascend(iterator btree.ItemIterator) {
	a.Mutex.Lock()
	defer a.Mutex.Unlock()
	a.BTree.Ascend(iterator)
}

func (a *AuditBtree) AscendGreaterOrEqual(pivot btree.Item, iterator btree.ItemIterator) {
	a.Mutex.Lock()
	defer a.Mutex.Unlock()
	a.BTree.AscendGreaterOrEqual(pivot, iterator)
}

func (a *AuditBtree) AscendLessThan(pivot btree.Item, iterator btree.ItemIterator) {
	a.Mutex.Lock()
	defer a.Mutex.Unlock()
	a.BTree.AscendLessThan(pivot, iterator)
}

func (a *AuditBtree) AscendRange(greaterOrEqual, lessThan btree.Item, iterator btree.ItemIterator) {
	a.Mutex.Lock()
	defer a.Mutex.Unlock()
	a.BTree.AscendRange(greaterOrEqual, lessThan, iterator)
}

func (a *AuditBtree) Clone() (t2 *btree.BTree) {
	a.Mutex.Lock()
	defer a.Mutex.Unlock()
	return a.BTree.Clone()
}

func (a *AuditBtree) DeleteMax() btree.Item {
	a.Mutex.Lock()
	defer a.Mutex.Unlock()
	return a.BTree.DeleteMax()
}

func (a *AuditBtree) Descend(iterator btree.ItemIterator) {
	a.Mutex.Lock()
	defer a.Mutex.Unlock()
	a.BTree.Descend(iterator)
}

func (a *AuditBtree) DescendGreaterThan(pivot btree.Item, iterator btree.ItemIterator) {
	a.Mutex.Lock()
	defer a.Mutex.Unlock()
	a.BTree.DescendGreaterThan(pivot, iterator)
}

func (a *AuditBtree) DescendLessOrEqual(pivot btree.Item, iterator btree.ItemIterator) {
	a.Mutex.Lock()
	defer a.Mutex.Unlock()
	a.BTree.DescendLessOrEqual(pivot, iterator)
}

func (a *AuditBtree) DescendRange(greaterOrEqual, lessThan btree.Item, iterator btree.ItemIterator) {
	a.Mutex.Lock()
	defer a.Mutex.Unlock()
	a.BTree.DescendRange(greaterOrEqual, lessThan, iterator)
}

func (a *AuditBtree) Get(key btree.Item) btree.Item {
	a.Mutex.Lock()
	defer a.Mutex.Unlock()
	return a.BTree.Get(key)
}

func (a *AuditBtree) Has(key btree.Item) bool {
	a.Mutex.Lock()
	defer a.Mutex.Unlock()
	return a.BTree.Has(key)
}

func (a *AuditBtree) Max() btree.Item {
	a.Mutex.Lock()
	defer a.Mutex.Unlock()
	return a.BTree.Max()
}

func (a *AuditBtree) Min() btree.Item {
	a.Mutex.Lock()
	defer a.Mutex.Unlock()
	return a.BTree.Min()
}
